class Post < ActiveRecord::Base
  has_many :photos, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  
  def add_photos(photos_to_add)
    Array(photos_to_add)
    photos_to_add.each do |photo|
      photos << photo
      puts "Added photo #{photo.inspect}."
    end
  end
  
  def self.find_last_post
    find(:first, :order => "id DESC")
  end
  
  def previous_post(dist = 1)
    if dist >= self.id then raise ActiveRecord::RecordNotFound end
    begin
      Post.find(self.id - dist)
    rescue ActiveRecord::RecordNotFound
      previous_post(dist + 1)
    end
  end
  
  def has_previous?
    begin
      self.previous_post
    rescue ActiveRecord::RecordNotFound
      false
    else
      true
    end
  end
  
  def next_post(dist = 1)
    if dist > Post.find(:first, :order => "id DESC").id then raise ActiveRecord::RecordNotFound end
    begin
      Post.find(self.id + dist)
    rescue ActiveRecord::RecordNotFound
      next_post(dist + 1)
    end
  end
  
  def has_next?
    begin
      self.next_post
    rescue ActiveRecord::RecordNotFound
      false
    else
      true
    end
  end
  
end
